package com.easybuy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.easybuy.Tools.Page;
import com.easybuy.pojo.Product;
import com.easybuy.service.ProductService.ProductService;
import org.apache.commons.io.FilenameUtils;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.html.parser.Entity;
import java.beans.Transient;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
//关于商品的操作
public class Member_ProductController {
    @Autowired
    ProductService productService;

    @RequestMapping("/productList")
    @ResponseBody
    //所有商品展示（分页）
    public Page productList(Page page){
        return productService.allProducts(page);
    }


    @RequestMapping("/checkProductName")
    @ResponseBody
    //通过商品名称查询（检查商品名称是否重复）
    public Product checkProductName(@RequestParam String name){
        return productService.selectProductByName(name);
    }


    @RequestMapping("/getProduct")
    @ResponseBody
    //通过id查询商品
    public Product geProduct(@RequestParam int id){
        return productService.selectProductById(id);
    }


    @RequestMapping("/addProduct")
    @ResponseBody
    //添加商品
    public Boolean addProduct(@RequestParam("file") MultipartFile file,@RequestParam("json") String json){
        try {
            Product product=JSON.parseObject(json,Product.class);
            String a=FilenameUtils.getExtension(file.getOriginalFilename());
            String fileName=UUID.randomUUID().toString()+a;
            System.out.println(fileName);
            product.setFileName(fileName);
            String path="E:\\Programmer Tools\\nginx-1.16.1\\html\\statics\\images\\"+fileName;
            File newFile = new File(path);
            file.transferTo(newFile);
            if (productService.addProduct(product)>0){
                return true;
            }else return false;
        } catch (IOException e) {
            System.out.println("Upload Defeated！");
            e.printStackTrace();
        }
        return false;
    }


    @RequestMapping("/modifyProduct")
    @ResponseBody
    //修改商品
    public boolean modifyProduct(@RequestParam(value = "file",required = false) MultipartFile file,@RequestParam("json") String json){
        try {
            Product product=(Product) JSON.parseObject(json,Product.class);
            if (file!=null){
                String a=FilenameUtils.getExtension(file.getOriginalFilename());
                String fileName=UUID.randomUUID().toString()+"."+a;
                product.setFileName(fileName);
                String path="E:\\Programmer Tools\\nginx-1.16.1\\html\\statics\\images\\"+fileName;
                File newFile = new File(path);
                file.transferTo(newFile);
            }
            if (productService.updateProduct(product)>0){
                return true;
            }else return false;
        } catch (IOException e) {
            System.out.println("Upload Defeated！");
            e.printStackTrace();
        }
        return false;
    }


    @RequestMapping("/deleProduct")
    @ResponseBody
    //删除商品
    public boolean deleProduct(int id){
        if (productService.deleProduct(id)>0){
            return true;
        }
        else return false;
    }

}
